package com.qin.addappwidget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

/**
 * 自定义 AppWidget
 * 
 * @author qinjuning
 */
public class MyAppWidgetProvider extends AppWidgetProvider {

	private static String TAG = "MyAppWidgetProvider";

	private static RemoteViews remoteViews = null;

	private static boolean isDefaultIcon = true; // 是否设置为默认图片

	private static int count = 0;

	// 每次删除该类型的窗口小部件(AppWidget)时都会触发 ，同时发送ACTION_APPWIDGET_DELETED广播，
	// 该广播可被onReceive()方法接受到.
	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		super.onDeleted(context, appWidgetIds);
		Log.i(TAG, "--- onDeleted ---");
	}

	// 最后删除该类型的窗口小部件(AppWidget)时触发
	@Override
	public void onDisabled(Context context) {
		super.onDisabled(context);
		Log.i(TAG, "--- onDisabled ---");
	}

	// 第一次添加该类型的窗口小部件窗口小部件(AppWidget)时触发
	@Override
	public void onEnabled(Context context) {
		super.onEnabled(context);
		Log.i(TAG, "--- onEnabled ---");
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		super.onReceive(context, intent);
		Log.i(TAG, "--- onReceive and action is ---" + intent.getAction());

		Log.i(TAG, "--- idDefaultIcon---" + isDefaultIcon);

		if (intent.getAction().equals("com.qin.action.CHANGE_IMAGE")) {

			AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

			// 设置为默认图片
			if (isDefaultIcon)
				remoteViews.setImageViewResource(R.id.img, R.drawable.icon);
			else
				remoteViews.setImageViewResource(R.id.img, R.drawable.girl);

			// 可以任意指定新的RemoteViews对象
			// remoteViews = new RemoteViews(context.getPackageName() ,
			// R.layout.main);

			appWidgetManager.updateAppWidget(new ComponentName(context, MyAppWidgetProvider.class), remoteViews);
			// 等同与下面这种方式
			// 获得所有该类型的窗口小部件id
			// int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new
			// ComponentName(context,MyAppWidgetProvider.class));
			// appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

			isDefaultIcon = !isDefaultIcon;
		}

	}

	// 每次添加一个该类型的窗口小部件窗口小部件(AppWidget)都会触发，同时发送ACTION_APPWIDGET_UPDATE广播
	// 一般在该函数为初始化添加的窗口小部件 , 即为它分配RemoteViews

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		count++;
		System.out.println("---------------- count -------- " + count);
		// TODO Auto-generated method stub
		super.onUpdate(context, appWidgetManager, appWidgetIds);

		RemoteViews remoteViews = updateWidgetView(context);

		// 对传递过来的参数appWidgetIds[]数组进行遍历更新/初始化
		int N = appWidgetIds.length;
		for (int i = 0; i < N; i++) {
			appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
		}
	}

	private RemoteViews updateWidgetView(Context context) {

		if (remoteViews == null)
			// 初始化remoteView对象
			remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_change_image);

		// 创建更改图片资源
		Intent resetIntent = new Intent("com.qin.action.CHANGE_IMAGE");
		PendingIntent resetNoPending = PendingIntent.getBroadcast(context, 0, resetIntent, 0);

		// 设置相应属性
		remoteViews.setOnClickPendingIntent(R.id.btn_resetNumber, resetNoPending);
		remoteViews.setImageViewResource(R.id.img, R.drawable.icon);

		return remoteViews;
	}

}
